package com.supergzt.controller;

import com.supergzt.annotation.RequiresLogin;
import com.supergzt.entity.base.Result;
import com.supergzt.entity.dos.User;
import com.supergzt.entity.vos.LoginVo;
import com.supergzt.entity.vos.RegisterVo;
import com.supergzt.service.KaptchaService;
import com.supergzt.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张钧
 * @Description
 * @create 2023-03-30 19:16
 */
@RestController
@RequestMapping("/auth")
public class AuthController {


	@Resource
	private KaptchaService kaptchaService;

	@Resource
	private UserService userService;

	/**
	 * 获取验证码图片
	 * @param response 响应体
	 * @param uuid uuid
	 * @throws IOException 异常
	 */
	@GetMapping("kaptcha")
	public void getKaptchaImage(HttpServletResponse response, @RequestParam(value = "uuid",required = false)String uuid) throws IOException {
		kaptchaService.getKaptchaImage(response,uuid);
	}

	@PostMapping("register")
	public Result<?> registerUser(@RequestBody RegisterVo registerVo){
		userService.registerUser(registerVo);
		return Result.ok();
	}

	@PostMapping("login")
	public Result<?> login(@RequestBody LoginVo loginVo) {
		return Result.success(userService.login(loginVo));
	}

	@DeleteMapping("logout")
	@RequiresLogin
	public Result<?> logout(HttpServletRequest request) {
		userService.logout(request);
		return Result.ok();
	}

}
